import { request } from "@/utils/request";
import type { LoginData } from "@/types/login";
import type { HttpResponse } from "@/types/request";
import type { UserInfo, UserDetailResult, LevelInfo } from "@/types/user";

interface LoginResult extends HttpResponse {
  profile: UserInfo;
  cookie: string;
}

interface VerifyCodeResult extends HttpResponse {
  data: boolean;
}

interface LevelResult extends HttpResponse {
  full: boolean;
  data: LevelInfo;
}

// 发送验证码
export const sendCaptcha = (data: {
  phone: string;
  ctcode: string;
}): Promise<HttpResponse> =>
  request({
    url: "/captcha/sent",
    method: "GET",
    data,
  });

// 验证验证码
export const verifyCaptcha = (data: {
  phone: string;
  ctcode: string;
  captcha: string;
}): Promise<VerifyCodeResult> =>
  request<VerifyCodeResult>({
    url: "/captcha/verify",
    method: "GET",
    data,
  });

// 手机登录
export const loginByPhone = (data: LoginData): Promise<LoginResult> =>
  request<LoginResult>({
    url: "/login/cellphone",
    method: "POST",
    data,
  });

// 获取用户详情
export const getUserDetail = (
  uid: string
): Promise<UserDetailResult & HttpResponse> =>
  request<UserDetailResult & HttpResponse>({
    url: `/user/detail?uid=${uid}`,
    method: "GET",
  });

// 获取账号信息
export const getUserInfo = (): Promise<HttpResponse> =>
  request({
    url: "/user/account",
    method: "GET",
  });

// 获取用户等级信息
export const getUserLevel = (): Promise<LevelResult> =>
  request<LevelResult>({
    url: "/user/level",
    method: "GET",
  });

// 获取用户信息 , 歌单，收藏，mv, dj 数量
export const getUserSubcount = (): Promise<HttpResponse> =>
  request<HttpResponse>({
    url: "/user/subcount",
  });

// 更新头像
export const updateUserAvatar = (data: FormData): Promise<HttpResponse> =>
  request({
    url: "/avatar/upload",
    method: "POST",
    headers: {
      "Content-Type": "multipart/form-data",
    },
    data,
  });
